package xin.yangshuai.activemqgeneral.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * JmsProducer
 *
 * @author shuai
 * @date 2020/6/21
 */
public class JmsProducer {

    public static final String ACTIVEMQ_URL = "tcp://192.168.124.7:61616";
    public static final String QUEUE_NAME = "queue01";
    public static final String QUEUE_MAP_MESSAGE_KEY_01 = "queue01_map_01";

    public static void main(String[] args) throws JMSException, InterruptedException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageProducer messageProducer = session.createProducer(session.createQueue(QUEUE_NAME));

        //开启持久化（队列默认的传送模式，保存未被消费的消息）
        //messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

        for (int i = 0; i < 30; i++) {
            Thread.sleep(1000);
            //textMessage
            TextMessage textMessage = session.createTextMessage("msg---" + (i + 1));

            textMessage.setStringProperty("member", "vip");

            messageProducer.send(textMessage);

            MapMessage mapMessage = session.createMapMessage();
            mapMessage.setString(QUEUE_MAP_MESSAGE_KEY_01, "msg---" + (i + 1));
            messageProducer.send(mapMessage);
        }

        messageProducer.close();
        session.close();
        connection.close();

        System.out.println("*****消息发布到MQ完成");

    }
}
